package com.fuyun.microservice.handler;

import com.fuyun.microservice.utils.Constants;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class ServerIdleStateHandler extends IdleStateHandler {
    /**
     * 设置空闲检测时间为 60s
     */
    public ServerIdleStateHandler() {
        super(Constants.HEARTBEART_EXPIRE_SECONDS, 0, 0, TimeUnit.SECONDS);
    }

    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) {
        log.info("{} 秒内没有读取到数据,关闭连接", Constants.HEARTBEART_EXPIRE_SECONDS);
        ctx.channel().close();
    }
}
